#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>

using namespace std;

string repeatLimitedString(string s, int repeatLimit) {
    int cnt[26]{};
    for (char c : s)
        cnt[c - 'a']++;

    string res;
    for (int i = 25, j = 24; i >= 0; i--)
    {
        j = min(j, i - 1);
        while (1)
        {
            for (int k = min(cnt[i], repeatLimit); k; k--)
            {
                res += 'a' + i;
                cnt[i]--;
            }
            if (cnt[i] == 0)
                break;
            while (j >= 0 && cnt[j] == 0)
                j--;
            if (j < 0)
                break;
            res += 'a' + j;
            cnt[j]--;
        }
    }
    return res;
}